from rest_framework import serializers
from . import models
from django.conf import settings


class ModulePageSerializers(serializers.ModelSerializer):
    project_info = serializers.SerializerMethodField(read_only=True)  # 项目表对应字段
    module_info = serializers.SerializerMethodField(read_only=True)  # 用户表对应字段
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)
    update_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)

    def get_project_info(self, obj):
        myobj = obj.project
        return {
            'id': myobj.id,
            'name': myobj.name,
            'status': myobj.status,
            'value': myobj.name
        }

    def get_module_info(self, obj):
        myobj = obj.module
        return {
            'id': myobj.id,
            'name': myobj.name,
            'status': myobj.status,
            'value': myobj.name
        }

    class Meta:
        model = models.ModulePage
        fields = "__all__"  # 代表获取所有字段
        # depth = 1  # 以外键关系向下找1层，数字代表层数
        extra_kwargs = {
            "project": {"write_only": True},
            "module": {"write_only": True},
        }


class LabelElementSerializers(serializers.ModelSerializer):
    project_info = serializers.SerializerMethodField(read_only=True)  # 项目表对应字段
    module_info = serializers.SerializerMethodField(read_only=True)  # 用户表对应字段
    module_page_info = serializers.SerializerMethodField(read_only=True)  # 用户表对应字段
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)
    update_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)

    def get_project_info(self, obj):
        myobj = obj.project
        return {
            'id': myobj.id,
            'name': myobj.name,
            'status': myobj.status,
            'value': myobj.name
        }

    def get_module_info(self, obj):
        myobj = obj.module
        return {
            'id': myobj.id,
            'name': myobj.name,
            'status': myobj.status,
            'value': myobj.name
        }

    def get_module_page_info(self, obj):
        myobj = obj.module_page
        return {
            'id': myobj.id,
            'title': myobj.title,
            'status': myobj.status,
            'value': myobj.title
        }

    class Meta:
        model = models.LabelElement
        fields = "__all__"  # 代表获取所有字段
        # depth = 1  # 以外键关系向下找1层，数字代表层数
        extra_kwargs = {
            "project": {"write_only": True},
            "module": {"write_only": True},
            "module_page": {"write_only": True},
        }


class StepsSerializers(serializers.ModelSerializer):
    project_info = serializers.SerializerMethodField(read_only=True)  # 项目表对应字段
    module_info = serializers.SerializerMethodField(read_only=True)  # 用户表对应字段
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)
    update_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)

    def get_project_info(self, obj):
        myobj = obj.project
        return {
            'id': myobj.id,
            'name': myobj.name,
            'status': myobj.status,
            'value': myobj.name
        }

    def get_module_info(self, obj):
        myobj = obj.module
        return {
            'id': myobj.id,
            'name': myobj.name,
            'status': myobj.status,
            'value': myobj.name
        }

    class Meta:
        model = models.Steps
        fields = "__all__"  # 代表获取所有字段
        # depth = 1  # 以外键关系向下找1层，数字代表层数
        extra_kwargs = {
            "project": {"write_only": True},
            "module": {"write_only": True},
        }


class SeleniumFuncSerializers(serializers.ModelSerializer):
    project_info = serializers.SerializerMethodField(read_only=True)  # 项目表对应字段
    module_info = serializers.SerializerMethodField(read_only=True)  # 用户表对应字段
    module_page_info = serializers.SerializerMethodField(read_only=True)  # 用户表对应字段
    sql_body_info = serializers.SerializerMethodField(read_only=True)  # sql表对应字段
    sql_assert_info = serializers.SerializerMethodField(read_only=True)  # sql表对应字段
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)
    update_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)

    def get_project_info(self, obj):
        myobj = obj.project
        if myobj:
            return {
                'id': myobj.id,
                'name': myobj.name,
                'status': myobj.status,
                'value': myobj.name
            }

    def get_module_info(self, obj):
        myobj = obj.module
        if myobj:
            return {
                'id': myobj.id,
                'name': myobj.name,
                'status': myobj.status,
                'value': myobj.name
            }

    def get_module_page_info(self, obj):
        myobj = obj.module_page
        if myobj:
            return {
                'id': myobj.id,
                'title': myobj.title,
                'status': myobj.status,
                'value': myobj.title
            }

    def get_sql_body_info(self, obj):  # 获取sql表字段的对应方法
        myobj = obj.sql_detail_body
        if not myobj:
            return None
        database = myobj.database
        db_types = settings.MODELS_CHOICES['database']['db_type']
        db_type = [db[1] for db in db_types if db[0] == database.db_type][0]
        return {
            'id': myobj.id,
            'sql_title': myobj.sql_title,
            'sql_template': myobj.sql_template,
            'db_type': db_type,
            'db_info': {
                'db': database.db_name,
                'host': database.db_ip,
                'port': database.db_port,
                'user': database.db_user,
                'password': database.db_password,
                'charset': 'utf8'
            },
            'status': myobj.status,
        }

    def get_sql_assert_info(self, obj):  # 获取sql表字段的对应方法
        myobj = obj.sql_detail_assert
        if not myobj:
            return None
        database = myobj.database
        db_types = settings.MODELS_CHOICES['database']['db_type']
        db_type = [db[1] for db in db_types if db[0] == database.db_type][0]
        return {
            'id': myobj.id,
            'sql_title': myobj.sql_title,
            'sql_template': myobj.sql_template,
            'db_type': db_type,
            'db_info': {
                'db': database.db_name,
                'host': database.db_ip,
                'port': database.db_port,
                'user': database.db_user,
                'password': database.db_password,
                'charset': 'utf8'
            },
            'status': myobj.status,
        }

    class Meta:
        model = models.SeleniumFunc
        fields = "__all__"  # 代表获取所有字段
        # depth = 1  # 以外键关系向下找1层，数字代表层数
        extra_kwargs = {
            "project": {"write_only": True},
            "module": {"write_only": True},
            "module_page": {"write_only": True},
        }


class StepsRealizeSerializers(serializers.ModelSerializer):
    project_info = serializers.SerializerMethodField(read_only=True)  # 项目表对应字段
    module_info = serializers.SerializerMethodField(read_only=True)  # 模块表对应字段
    module_page_info = serializers.SerializerMethodField(read_only=True)  # 页面表对应字段
    # label_element_info = serializers.SerializerMethodField(read_only=True)  # 标签表对应字段
    steps_info = serializers.SerializerMethodField(read_only=True)  # 步骤表对应字段
    selenium_func_info = serializers.SerializerMethodField(read_only=True)  # 执行函数表对应字段
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)
    update_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S', default=None)

    def get_project_info(self, obj):
        myobj = obj.project
        if not myobj:
            return
        return {
            'id': myobj.id,
            'name': myobj.name,
            'project_key':myobj.project_key,
            'status': myobj.status,
            'value': myobj.name
        }

    def get_module_info(self, obj):
        myobj = obj.module
        if not myobj:
            return
        return {
            'id': myobj.id,
            'name': myobj.name,
            'module_key': myobj.module_key,
            'status': myobj.status,
            'value': myobj.name
        }

    def get_module_page_info(self, obj):
        myobj = obj.module_page
        if not myobj:
            return
        return {
            'id': myobj.id,
            'title': myobj.title,
            'module_page_key': myobj.module_page_key,
            'status': myobj.status,
            'value': myobj.title
        }

    def get_steps_info(self,obj):
        myobj = obj.steps
        return {
            'id': myobj.id,
            'title': myobj.title,
            'status': myobj.status,
            'value': myobj.title
        }

    def get_selenium_func_info(self,obj):
        myobj = obj.selenium_func
        sql_detail_body_obj = myobj.sql_detail_body
        sql_detail_assert_obj = myobj.sql_detail_assert
        return {
            'id': myobj.id,
            'title': myobj.title,
            'func_name':myobj.func_name,
            'sql_detail_body': {
                'status': 1 if sql_detail_body_obj else 0,
                'sql':sql_detail_body_obj.sql_template if sql_detail_body_obj else None
            },
            'sql_detail_assert': {
                'status': 1 if sql_detail_assert_obj else 0,
                'sql':sql_detail_assert_obj.sql_template if sql_detail_assert_obj else None
            },
            'status': myobj.status,
            'value': myobj.title
        }

    class Meta:
        model = models.StepsRealize
        fields = "__all__"  # 代表获取所有字段
        # depth = 1  # 以外键关系向下找1层，数字代表层数
        extra_kwargs = {
            "project": {"write_only": True},
            "module": {"write_only": True},
            "module_page": {"write_only": True},
            "steps": {"write_only": True},
            "selenium_func": {"write_only": True},
            "sql_detail_body": {"write_only": True},
            "sql_detail_assert": {"write_only": True},
        }
